package cn.itcast.service.targo.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service(timeout = 100000)
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractDao contractDao;


    /*
    分页查询
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo<>(list);
    }

    /*
    查询所有附件
     */
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        return extCproductDao.selectByExample(extCproductExample);
    }

    //通过id查询附件
    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }



    /*
   附件添加
    */
    @Override
    public void save(ExtCproduct extCproduct) {
        //1.设置主键
        extCproduct.setId(UUID.randomUUID().toString());
        //2.计算附件金额
        Double amount = 0d;
        if(extCproduct.getPrice() != null && extCproduct.getCnumber()!=null ){
            amount = extCproduct.getPrice() * extCproduct.getCnumber();
        }
        //3.设置附件金额
        extCproduct.setAmount(amount);


        //4.1修改购销合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //4.2设置购销合同总金额 = 总金额 + 附件金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //4.3设置购销合同附件数量
        contract.setExtNum(contract.getExtNum() + 1);

        //4.4执行修改购销合同（动态修改）
        contractDao.updateByPrimaryKeySelective(contract);

        //5.附件添加
        extCproductDao.insertSelective(extCproduct);
    }

    //附件修改
    @Override
    public void update(ExtCproduct extCproduct) {
        //1. 修改后的附件金额
        Double amount = 0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);


        //2.根据附件id查询修改前的金额
        Double oldAmount = extCproductDao.selectByPrimaryKey(extCproduct.getId()).getAmount();
        //根据购销合同id查询
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //设置购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount - oldAmount);


        //执行更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

        //执行更新附件信息
        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

     @Override
    public void delete(String id) {
        //1. 根据附件id查询获取附件金额
        ExtCproduct extCproduct = extCproductDao.selectByPrimaryKey(id);
        Double extAmount = extCproduct.getAmount();

        //4. 修改购销合同
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        //4.1 总金额 = 总金额 - 附件金额 - 附件金额
        contract.setTotalAmount(contract.getTotalAmount() - extAmount);
        //4.2 附件数量 = 附件数量 - 1
        contract.setExtNum(contract.getExtNum() - 1);

        //4.4 修改
        contractDao.updateByPrimaryKeySelective(contract);

        //5. 删除附件
        extCproductDao.deleteByPrimaryKey(id);
    }





}
